<?php
namespace app\api\controller;

use app\api\logic\LotteryLogic;
use app\BaseController;
use think\api\Client;
use think\facade\Config;
use think\facade\Db;

class Index extends BaseController {
    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct() {
        $this->app_code = Config::get('app.app_code');
    }
    public function index() {
        $num = 1600297200 % 86400;
        echo $num;exit;
        echo 'ThinkPHP6';
    }
    /*身份证查询*/

    public function get_idcard() {
        $idcard = '450923199011193070';
        $client = new Client($this->app_code);

        $result = $client->idcardIndex()
            ->withCardno($idcard)
            ->request();

        return json($result);
    }
/*天气预报*/
    public function weather() {
        $client = new Client($this->app_code);

        $result = $client->WeatherQuery()
            ->withCity('南宁')
            ->request();
        dump($result);
    }
    /*新闻头条*/
    public function toutiao() {
        $client = new Client($this->app_code);

        $result = $client->newsToutiao()
            ->withType('yule')
            ->request();
        dump($result);
    }
    /*彩种*/
    public function lottery() {
        $client = new Client($this->app_code);

        $result = $client->lotteryTypes()
            ->request();
        $list = $result['data'];
        foreach ($list as $key => $data) {
            $lottery = Db::name('lottery')->where('lottery_name', $data['lottery_name'])->find();
            if ($lottery) {
                continue;
            }

            Db::name('lottery')->insert($data);
        }

        dump($result);
    }
    /*购买彩种*/
    public function lottery_buy($lottery_id = 'ssq') {

        $bonus_logic = new LotteryLogic();
        $result      = $bonus_logic->lottery_buy($lottery_id);

        dump('购买成功');
    }
    /*彩票开奖结果查询*/
    public function query($lottery_id = 'ssq') {
        $client = new Client($this->app_code);

        $result = $client->lotteryQuery()
            ->withLotteryId($lottery_id)
            ->request();
        dump($result);
    }
    /*历史开奖结果查询*/
    public function history($lottery_id = 'ssq') {
        $client = new Client($this->app_code);

        $result = $client->lotteryHistory()
            ->withLotteryId($lottery_id)
            ->withPagesize('2')
            ->request();
        dump($result);
    }
    /*中奖计算器*/
    public function bonus($lottery_id = 'ssq') {
        $bonus_logic = new LotteryLogic();
        $result      = $bonus_logic->bonus($lottery_id);

        dump('没有开奖');

    }
    /*
     *开奖记录
     */
    public function lottery_result($dataType = 'all') {
        if ($dataType == 'ssq') {
            $where['lottery_id'] = 'ssq';

        } elseif ($dataType == 'delivery') {
            $where[] = ['prize_status', '=', 1];

        } else {
            $where['lottery_id'] = 'ssq';
        }

        $data = Db::name('lottery_log')
            ->where($where)->order('open_time desc')
            ->paginate(10)->each(function ($item, $key) {
            $item['create_time'] = Date('Y-m-d', $item['open_time']);
            $item['prize_msg']   = $item['lottery_res'];
            $item['lottery_no']  = $item['lottery_no'];
            $res                 = explode(',', $item['lottery_res']);
            unset($res[6]);
            $item['sum'] = array_sum($res);
            return $item;
        });
        return json($data);
    }

    /*
     *获取彩票记录
     */
    public function lottery_log($dataType = 'all') {
        if ($dataType == 'payment') {
            $where[] = ['status', '=', 0];

        } elseif ($dataType == 'delivery') {
            $where[] = ['prize_status', '=', 1];

        } else {
            $where[] = ['create_time', '>', 0];
        }

        $data = Db::name('lottery_bonus')->where($where)->order('id desc')->paginate(10)->each(function ($item, $key) {
            $item['create_time'] = Date('Y-m-d', $item['create_time']);
            $item['prize_msg']   = $item['real_lottery_res'] ? $item['prize_msg'] : '未开奖';
            return $item;
        });
        return json($data);
    }

    function getpao($arr) {
        $len = count($arr);
        //设置一个空数组 用来接收冒出来的泡
        //该层循环控制 需要冒泡的轮数
        for ($i = 1; $i < $len; $i++) {
            //该层循环用来控制每轮 冒出一个数 需要比较的次数
            for ($k = 0; $k < $len - $i; $k++) {
                if ($arr[$k] > $arr[$k + 1]) {
                    $tmp         = $arr[$k + 1];
                    $arr[$k + 1] = $arr[$k];
                    $arr[$k]     = $tmp;
                }
            }
        }
        return $arr;
    }

}
